package com.zkgengkun.console.controller;

import com.zkgengkun.constant.MapConstants;
import com.zkgengkun.support.util.CookieUtils;
import com.zkgengkun.support.util.JedisUtil;
import com.zkgengkun.support.util.JsoupUtil;
import com.zkgengkun.support.util.UUIDUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by Shik on 2017/5/14.
 */
@Controller
@RequestMapping(value = "book")
public class BookController {

    @RequestMapping(value = {"", "index"}, method = RequestMethod.GET)
    public String index(HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie = CookieUtils.getCookieByName(request, "book_user_id");
        if (cookie == null) {
            CookieUtils.addCookie(response, "book_user_id", UUIDUtils.random32UUID(), 0);
        }
        return "web/book/index";
    }

    @RequestMapping(value = "search")
    public String bookSearch(String query, HttpServletRequest request, Model model) {
        String url = MapConstants.BOOK_MAP.get("23us" + "_search");
        try {
            Document doc = JsoupUtil.getDocument(url + query);
            Elements elements = doc.getElementsByClass("result-game-item-title-link");
            Element element = elements.get(0);
            doc = JsoupUtil.getDocument(element.attr("href"));
            JedisUtil.getInstance().STRINGS.set(CookieUtils.getCookieByName(request, "book_user_id").getValue(), element.attr("href"));
            elements = doc.getElementsByTag("td");
            model.addAttribute("elements", elements);
            model.addAttribute("bookname", element.attr("title"));

        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:index";
        }
        return "web/book/directory_list";
    }

    @RequestMapping(value = "/{itemNo}")
    public String bookItem(@PathVariable String itemNo, Model model, HttpServletRequest request) {
        try {
            Document doc = JsoupUtil.getDocument(
                    JedisUtil.getInstance().STRINGS.get(
                            CookieUtils.getCookieByName(request, "book_user_id").getValue()) + itemNo + ".html");
            Element content = doc.getElementById("contents");
            Elements elements = doc.getElementsByTag("dl");
            System.out.println(elements.get(0).child(1));
            Element bookname = elements.get(0).child(1);
            model.addAttribute("content", content);
            model.addAttribute("bookname", bookname);
            model.addAttribute("itemNo", itemNo);
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:index";
        }

        return "web/book/book_item";
    }

    @RequestMapping(value = "qzgs_list")
    public String qzgsList(Model model) {
        try {
            Document doc = JsoupUtil.getDocument("http://www.qu.la/book/32/");
            Elements elements = doc.getElementById("list").getElementsByTag("dd");
            model.addAttribute("elements", elements);
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:index";
        }

        return "web/book/qzgs";
    }

    @RequestMapping(value = "/qzgs/{itemNo}")
    public String qzgsItem(@PathVariable String itemNo, Model model) {
        Document doc = null;
        try {
            doc = JsoupUtil.getDocument("http://www.qu.la/book/32/" + itemNo + ".html");
            Element content = doc.getElementById("content");
            Elements elements = doc.getElementsByClass("bookname");
            Element bookname = elements.get(0).child(0);
            model.addAttribute("content", content);
            model.addAttribute("bookname", bookname);
            model.addAttribute("itemNo", itemNo);
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:index";
        }

        return "web/book/qzgs_item";
    }

}
